//logs.js
import store from "../../utils/factory";
import {
  rechargeList,
  recharge,
  getWallet
} from "../../utils/config";
//获取应用实例
const app = getApp();
let that = null;

Page({
  data: {
    dialog1: false,
    list: [],
    active: 0,
    money: 0,
    yue: 0,
  },
  onLoad: function (e) {
    that = this;
  },
  onShow() {
    this.rechargeList();
    this.getWallet()
  },
  open() {
    this.setData({
      dialog1: true
    })
  },
  close() {
    this.setData({
      dialog1: false
    })
  },
  getWallet() {
    getWallet().then(res => {
      this.setData({
        yue: res.data.wallet
      })
    })
  },
  payWX(data) {
    wx.hideLoading();
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.packageValue,
      signType: "MD5",
      paySign: data.paySign,
      success: function (res) {
        wx.showModal({
          title: "温馨提示",
          content: "充值成功",
          showCancel: false, //是否显示取消按钮
          success: function (res) {
            that.setData({
              dialog1: false
            })
            that.getWallet()
          }
        });
      },
      fail: function (res) {
        wx.showToast({
          icon: "none",
          title: "充值失败"
        })
      },
      complete: function (res) {}
    });
  },
  choose(e) {
    let money = e.currentTarget.dataset.value;
    let index = e.currentTarget.dataset.index;
    this.setData({
      active: index,
      money: money
    })
    console.log(money)
  },
  recharge() {
    wx.showLoading({
      title:"加载中..."
    })
    recharge({
      value: this.data.money
    }).then(res => {
      this.payWX(res.data)
    })
  },
  rechargeList() {
    rechargeList({}).then(res => {
      this.setData({
        list: res.rows,
        money: res.rows[0]
      })
    });
  }
});